/*###################################################################*
#
# MP (Mitchell Pell) Base - CrossPlatform Base Utilities.
# Copyright (C) 2014  Mitchell Pell
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################*/
#ifndef NETWORK_MANAGER_H
#define NETWORK_MANAGER_H

#include <vector>
#include "TcpServer.h"
#include "TcpClient.h"
#include "../../../GLDriver/GLDriverComponents/Console/GLConsole.h"
#include "../../../Base/Updateable.h"

#define NETWORK_MANAGER_DEBUG 3

using namespace mp_base;
using namespace boost::asio;
using mpgl_engine::driver::gl_console::GlConsole;

namespace mpgl_engine{ namespace application{
		/*! @brief Application level, Network Manager. */
		namespace net {

	/*!----------------------------------------------------------------
	*	@class		NetworkManager
	*	@brief		The main Network Manager responsible for managing
	*				the boost IO Service as well as the current
	*				NetSession.
	*
	*	@note		This component receives updates from either the 
	*				main thread or from a separate, networking, thread.
	*
	*	@author		Mitchell Pell
	*	@date		9/28/2013
	*	@version	1.0
	*
	----------------------------------------------------------------*/
	class NetworkManager : public Updateable
	{
	public:

		NetworkManager(GlConsole* console = NULL,
			TcpSessionType server = TcpSessionType::UNKNOWN);
		~NetworkManager();


		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Updateable Object Management
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		void Initialize();
		void Uninitialize();
		
		void Update(); //Optionally called by separate, 
						//networking, thread.

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Console Management
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		void SetConsole(GlConsole* console = NULL){ 
			m_console = console;}

	private:

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Sessions Vector Management
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		void SetTcpSession(TcpSession* session){
			m_session = session;
			if (m_console != NULL){
				m_session->SetConsole(m_console);
			}
		}
		TcpSession* GetTcpSession() const  { return m_session; }

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Members
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		/*! Boost ASIO IO Service. */
		boost::asio::io_service*	m_ioService;
		/*! 
			@brief	Reference to the Engines console. 
			@Note	May be moved to an interface that
					represents objects using a console.
		*/
		GlConsole*					m_console;
		/*! Is this client a server? */
		TcpSessionType				m_sessionType;
		/*! Array of open sessions. */
		TcpSession*					m_session;

	};
}}}
#endif